﻿namespace _5_NovaAdmin.Infrastructure.Extensions;

public static class GenericTypeExtensions
{
    /// <summary>
    /// 获取类型的泛型名称
    /// </summary>
    /// <param name="type">要获取名称的类型</param>
    /// <returns>类型的泛型名称</returns>
    public static string GetGenericTypeName(this Type type)
    {
        var typeName = string.Empty;

        if (type.IsGenericType)
        {
            var genericTypes = string.Join(",", type.GetGenericArguments().Select(t => t.Name).ToArray());
            typeName = $"{type.Name.Remove(type.Name.IndexOf('`'))}<{genericTypes}>";
        }
        else
        {
            typeName = type.Name;
        }

        return typeName;
    }

    /// <summary>
    /// 获取对象的泛型类型名称
    /// </summary>
    /// <param name="obj">要获取类型名称的对象</param>
    /// <returns>对象的泛型类型名称</returns>
    public static string GetGenericTypeName(this object obj)
    {
        return obj.GetType().GetGenericTypeName();
    }
}